import common from '../vendor/common'
import '../scss/user_browsing_history.scss'
import { browse } from '../vendor/api'
try{
    const mui = common.mui
    const Vue = common.Vue
    const houseList = common.houseList
    var VueList = null //房源列表Vue对象
    var data = {}
    // 下拉上拉插件初始化
    mui.init({
        swipeBack: false,
        pullRefresh: {
          container: '#pullrefresh',
          down: {
            style:'circle',
            callback: init_index
          },
          up: {
            height:50,//可选.默认50.触发上拉加载拖动距离
            auto:true,//可选,默认false.自动上拉加载一次
            contentrefresh : "正在加载...",//可选，正在加载状态时，上拉加载控件上显示的标题内容
            contentnomore:'没有更多数据了',//可选，请求完毕若没有更多数据时显示的提醒内容；
            callback: pullupRefresh
          }
        }
    });
    mui.plusReady(function(){
        VueList = new Vue({
            el: '#houseList',
            data: {
                list: [],
                page: 1,
                nothing: false
            },
            components: { houseList }
        })
        window.addEventListener('browse',function(e){
            VueList.list = []
            VueList.page = 1
            data.page = VueList.page
            browse(data).then((res) => {
                if (res.data.data.length === 0) {
                    mui('#pullrefresh').pullRefresh().endPullupToRefresh(true)
                    VueList.nothing = true
                    document.querySelectorAll('div.mui-pull-caption.mui-pull-caption-nomore')[0].innerHTML = ''
                }
                for (var p in res.data.data) {
                    if (res.data.data[p].houses) {
                        VueList.list.push(res.data.data[p].houses)
                    }
                }

                if (res.data.data.length < res.data.per_page) {
                    mui('#pullrefresh').pullRefresh().endPullupToRefresh(true)
                    document.querySelectorAll('div.mui-pull-caption.mui-pull-caption-nomore')[0].innerHTML = ''
                    return
                }
                VueList.page = res.data.current_page + 1
            })
        })
    })     
}catch(e){
    console.log('user_browsing_history.html出错了')
}
// 初始化页面数据
function init_index(){
    VueList.list = []
        VueList.page = 1
        data.page = VueList.page
        VueList.nothing = false
        browse(data).then((res) => {
            mui('#pullrefresh').pullRefresh().endPulldownToRefresh() //下拉刷新
            if (res.data.data.length === 0) {
                mui('#pullrefresh').pullRefresh().endPullupToRefresh(true)
                VueList.nothing = true
                document.querySelectorAll('div.mui-pull-caption.mui-pull-caption-nomore')[0].innerHTML = ''
                return
            }
            for (var p in res.data.data) {
                if (res.data.data[p].houses) {
                    VueList.list.push(res.data.data[p].houses)
                }
            }
            if (res.data.data.length < res.data.per_page) {
                mui('#pullrefresh').pullRefresh().endPullupToRefresh(true)
                document.querySelectorAll('div.mui-pull-caption.mui-pull-caption-nomore')[0].innerHTML = ''
                return
            }
            VueList.page = res.data.current_page + 1
            mui('#pullrefresh').pullRefresh().refresh(true) //打开下拉刷新
    })
}
function pullupRefresh(){
    mui('#pullrefresh').pullRefresh().endPullupToRefresh(false)
    data.page = VueList.page
    browse(data).then(res => {
        if (res.data.data.length === 0) {
            mui('#pullrefresh').pullRefresh().endPullupToRefresh(true)
            return
        }
        for (var p in res.data.data) {
            if (res.data.data[p].houses) {
                VueList.list.push(res.data.data[p].houses)
            }
        }
        VueList.page = res.data.current_page + 1
    })
}